package com.bbcare.department.plat.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.bbcare.comm.DateUtil;
import com.bbcare.comm.SeqUtil;
import com.bbcare.department.plat.entity.Contact;
import com.bbcare.department.plat.service.IDepartmentContactService;
import com.bbcare.department.plat.store.ibatis.IContactDao;

@Service
public class DepartmentContactService implements IDepartmentContactService {
	
	Logger logger = Logger.getLogger(DepartmentContactService.class); 
	
	@Autowired
	private IContactDao contactDao;
	
	/**
	 * 查询科室所有联系方式
	 * @param arg
	 * @return
	 * @throws Exception 
	 */
    public List<Contact> getContactsByDeptId(String deptId) throws Exception {
        logger.info("in getContactsByDeptId----arg==" + deptId);
        List<Contact> contacts =  contactDao.selectByDepId(deptId);
        logger.info("out getContactsByDeptId----arg==");
        return contacts;
    }
    
    /**
     * 查询单个联系方式
     */
    public Contact getContactById(String id) throws Exception{
    	logger.info("in getContactById" );
        Contact contact =  contactDao.selectById(id);
        logger.info("out getContactById");
        return contact;
    }

	@Override
	public int deleteContactByIds(List<String> list) throws Exception {
		logger.info("in deleteContactById");
        int i =  contactDao.deleteyIds(list);
        logger.info("out deleteContactById");
        return i;
	}

	@Override
	@Transactional
	public String contactMoidify(List<Contact> contacts) throws Exception {
		String now = DateUtil.getSysDateA();
		List<Contact> addList = new ArrayList<Contact>();
		
		int j = 0;
		for(int i = 0 ;i<contacts.size();i++){
			if(StringUtils.isEmpty(contacts.get(i).getId())){
				contacts.get(i).setId(SeqUtil.getSeqNo());
				contacts.get(i).setCreateTime(now);
				addList.add(contacts.get(i));
//				int m = contactDao.save(contacts.get(i));
			}else{
				contacts.get(i).setUpdateTime(now);
				j+= contactDao.update(contacts.get(i));
//				j+= n;
			}
		}
		
		if(!CollectionUtils.isEmpty(addList)){
			j+= contactDao.save(addList);
		}
		
		//sql里面批量插入多条
		return String.valueOf(j);
	}
    
    /**
     * 
     */

}
